﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Interface;
using NetDon.OA.Interface.Common;
using NetDon.Utilities;

namespace NetDon.OA.Portal.Admin
{
    public partial class ShuntingCostEdit : PageBase
    {
        #region Properties
        protected Guid Id
        {
            get
            {
                return Parameters.GetGuid("Id");
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
      
            if (!IsPostBack)
            {

                CancelButton = btnReturn;


                if (Id != Guid.Empty)
                {
                    var service = ServiceContainer.GetService<IShuntingCostConfigService>();
                    var entity = service.GetShuntingCostConfigById(Id);

                    panelCustomInfo.DataSource = entity;
                    panelCustomInfo.DataBind();
                    ddlCheckoutType.SelectedValue = entity.CheckoutType.ToString();
                    this.ddlRouteTypeId.SelectedValue = entity.RouteTypeId.ToString();
                    this.ucProvinceCity1.ProvinceCode = entity.DeliveryProvince;
                    this.ucProvinceCity1.CityCode = entity.DeliveryCity;



                }
            }
        }


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);



            ObjectDataSourceRoutes.Selecting += (ds, dsArgs) =>
            {
                dsArgs.InputParameters["areaId"] = CurrentApplicationContext.CompanyId.ToString();
            };
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {

            var customerId = Id;


            var service = ServiceContainer.GetService<IShuntingCostConfigService>();


            bool existName = false;
            existName = service.CheckShuntingCostConfigDataExists(Id, ddlRouteTypeId.SelectedValue.ToGuid(), ucProvinceCity1.CityCode, CurrentApplicationContext.CompanyId, (int)EnumUtilities.Parse<CheckoutType>(ddlCheckoutType.SelectedValue));

            if (existName)
            {
                Alert("此调车成本信息已存在，请重新输入！");
                return;
            }
            if (Id == Guid.Empty)
            {
                customerId = Guid.NewGuid();
            }
            var data = new ShuntingCostConfigData
            {
                Id = customerId,

                AreaId = CurrentApplicationContext.CompanyId,
                RouteTypeId = ddlRouteTypeId.SelectedValue.ToGuid(),
                DeliveryProvince = ucProvinceCity1.ProvinceCode,
                DeliveryCity = ucProvinceCity1.CityCode,
                ShuntingCost = decimal.Parse(this.txtShuntingCost.Text),
                UnitCost = decimal.Parse(this.txtUnitCost.Text),
                CheckoutType = EnumUtilities.Parse<CheckoutType>(ddlCheckoutType.SelectedValue)

            };

            bool result = Id == Guid.Empty ? service.AddShuntingCostConfig(data) : service.ModifyShuntingCostConfig(data);
            if (result)
            {
                Success("保存成功!");
                CloseWindow(true);
            }
            else
            {
                ShowDataError(data);
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
          {

              this.CloseWindow(false);

          }
    }

}